package com.model
{
	import com.net.PhpNet;
	import com.net.Share;
	import com.vo.CarVo;
	import com.vo.KindVo;
	import com.vo.MeshVo;
	import com.vo.PartVo;
	import com.vo.SectionVo;
	import com.vo.TypeVo;
	
	import flash.utils.Dictionary;
	
	/**
	 * 静态数据管理器
	 */	
	public class CarDataRemote
	{
		
		//		return;
		//		var carList:Vector.<CarVo> = CarDataRemote.carList;
		//		var menuLength:int = MenuXMLData.menuLength;
		//		for (var i:int = carList.length - 1; i >= 0; i--) 
		//		{
		//			var item:CarVo = carList[i];
		//			for (var j:int = 0; j < menuLength; j++) 
		//			{
		//				var idList:Vector.<String> = MenuXMLData.getItemIDList(j);
		//				for each (var id:String in idList) 
		//				{
		//					//getPartList($carID,$sectionID,$itemID){
		//					PhpNet.call(NetConfig.PART_LIST,onResultPart,item.carID,j,id);
		//				}
		//			}
		//		}
		private static var onComplete:Function;
		/**
		 * 向服务器发送请求(还未做本地保存) 开始获取目标数据 获取结束后回调
		 * @param onComplete
		 */		
		public static function start(onComplete:Function = null):void{
//			trace(int(Share.read(NetConfig.CAR_LIST + NetConfig.VERSION)));
			PhpNet.call(NetConfig.KIND_LIST,onResultKind,int(Share.read(NetConfig.KIND_LIST + NetConfig.VERSION)));
			//是不是第一次
			PhpNet.call(NetConfig.TYPE_LIST,onResultType,int(Share.read(NetConfig.TYPE_LIST + NetConfig.VERSION)));
			PhpNet.call(NetConfig.CAR_LIST,onResultCar,int(Share.read(NetConfig.CAR_LIST + NetConfig.VERSION)));
			PhpNet.call(NetConfig.PART_LIST,onResultPart,int(Share.read(NetConfig.PART_LIST + NetConfig.VERSION)));
			PhpNet.call(NetConfig.MESH_LIST,onResultMesh,int(Share.read(NetConfig.MESH_LIST + NetConfig.VERSION)));
			PhpNet.addCompleteEvent(onComplete);
			PhpNet.addErrorEvent(onError);
			CarDataRemote.onComplete = onComplete;
			//			if(Share.hasKey(NetConfig.KIND_LIST)){
			//				saveKind(Share.read(NetConfig.KIND_LIST));
			//			}else{
			//				PhpNet.call(NetConfig.KIND_LIST,onResultKind);
			//				var isRemote:Boolean = true;
			//			}
			//			if(Share.hasKey(NetConfig.TYPE_LIST)){
			//				saveType(Share.read(NetConfig.TYPE_LIST));
			//			}else{
			//				PhpNet.call(NetConfig.TYPE_LIST,onResultType);
			//				isRemote = true;
			//			}
			//			if(Share.hasKey(NetConfig.CAR_LIST)){
			//				saveCar(Share.read(NetConfig.CAR_LIST));
			//			}else{
			//				PhpNet.call(NetConfig.CAR_LIST,onResultCar);
			//				isRemote = true;
			//			}
			//			if(Share.hasKey(NetConfig.PART_LIST)){
			//				savePart(Share.read(NetConfig.PART_LIST));
			//			}else{
			//				PhpNet.call(NetConfig.PART_LIST,onResultPart);
			//				isRemote = true;
			//			}
			//			if(isRemote){
			//			PhpNet.addCompleteEvent(onComplete);
			//			}else{
			//				if(onComplete != null)onComplete();//直接回调
			//			}
			
		}
		private static var isStart:Boolean;//记录游戏开始启动了
		/**
		 * 链接出错了直接读cache
		 */		
		private static function onError():void
		{
			if(isStart)return;//已经开始了
			if(Share.hasKey(NetConfig.KIND_LIST)){
				saveKind(Share.read(NetConfig.KIND_LIST));
			}
			if(Share.hasKey(NetConfig.TYPE_LIST)){
				saveType(Share.read(NetConfig.TYPE_LIST));
			}
			if(Share.hasKey(NetConfig.CAR_LIST)){
				saveCar(Share.read(NetConfig.CAR_LIST));
			}
			if(Share.hasKey(NetConfig.PART_LIST)){
				savePart(Share.read(NetConfig.PART_LIST));
			}
			if(Share.hasKey(NetConfig.MESH_LIST)){
				saveMesh(Share.read(NetConfig.MESH_LIST));
			}
			isStart = true;
			if(onComplete != null)onComplete();
		}
		
		private static function onResultKind(obj:Object):void
		{
			if(obj.faultId != null){
				//				Alert.show('提交失败信息:'+ obj.faultId);
			}else{
				var list:Array = obj.data;
				if(list == null){
					saveKind(Share.read(NetConfig.KIND_LIST));//自己读cache
				}else{
					Share.wirte(NetConfig.KIND_LIST + NetConfig.VERSION,list[NetConfig.VERSION]);
					//保存版本号
					delete list[NetConfig.VERSION];
					list.reverse();
					saveKind(list);
					Share.wirte(NetConfig.KIND_LIST,list);//保存在本地
				}
			}
		}
		
		private static function saveKind(list:Array):void{
			for each (var data:Object in list) 
			{
				CarDataRemote.addKindVo(data);
			}
		}
		
		private static function onResultType(obj:Object):void
		{
			if(obj.faultId != null){
				//				Alert.show('提交失败信息:'+ obj.faultId);
			}else{
				var list:Array = obj.data;
				if(list == null){
					saveType(Share.read(NetConfig.TYPE_LIST));
				}else{
					Share.wirte(NetConfig.TYPE_LIST + NetConfig.VERSION,list[NetConfig.VERSION]);
					//保存版本号
					delete list[NetConfig.VERSION];
					saveType(list);
					Share.wirte(NetConfig.TYPE_LIST,list);//保存在本地
				}
			}
		}
		private static function saveType(list:Array):void{
			for each (var data:Object in list) 
			{
				CarDataRemote.addTypeVo(data);
			}
		}
		
		private static function onResultCar(obj:Object):void
		{
			if(obj.faultId != null){
				//				Alert.show('提交失败信息:'+ obj.faultId);
			}else{
				var list:Array = obj.data;
				if(list == null){
					saveCar(Share.read(NetConfig.CAR_LIST));
				}else{
					Share.wirte(NetConfig.CAR_LIST + NetConfig.VERSION,list[NetConfig.VERSION]);
					//保存版本号
					delete list[NetConfig.VERSION];
					saveCar(list);
					Share.wirte(NetConfig.CAR_LIST,list);//保存在本地
				}
			}
		}
		
		private static function saveCar(list:Array):void{
			for each (var data:Object in list) 
			{
				CarDataRemote.addCarVo(data);
			}
		}
		
		private static function onResultPart(obj:Object):void
		{
			if(obj.faultId != null){
				//				Alert.show('提交失败信息:'+ obj.faultId);
			}else{
				var list:Array = obj.data;
				if(list == null){
					savePart(Share.read(NetConfig.PART_LIST));
				}else{
					Share.wirte(NetConfig.PART_LIST + NetConfig.VERSION,list[NetConfig.VERSION]);
					//保存版本号
					delete list[NetConfig.VERSION];
					savePart(list);
					Share.wirte(NetConfig.PART_LIST,list);//保存在本地
				}
			}
		}
		
		private static function savePart(list:Array):void
		{
			for each (var data:Object in list) 
			{
				CarDataRemote.addPartVo(data);
			}
		}
		
		private static function onResultMesh(obj:Object):void
		{
			if(obj.faultId != null){
				//				Alert.show('提交失败信息:'+ obj.faultId);
			}else{
				var list:Array = obj.data;
				if(list == null){
					saveMesh(Share.read(NetConfig.MESH_LIST));
				}else{
					Share.wirte(NetConfig.MESH_LIST + NetConfig.VERSION,list[NetConfig.VERSION]);
					//保存版本号
					delete list[NetConfig.VERSION];
					saveMesh(list);
					Share.wirte(NetConfig.MESH_LIST,list);//保存在本地
				}
			}
		}
		
		private static function saveMesh(list:Array):void
		{
			for each (var data:Object in list) 
			{
				CarDataRemote.addMeshVo(data);
			}
		}
		
		private static var sectionList:Vector.<SectionVo>;
		public static function getSectionList():Vector.<SectionVo>
		{
			if(sectionList == null){
				sectionList = new Vector.<SectionVo>();
				MenuXMLData.getMenuTitle(onTitle);
			}
			return sectionList;
		}
		private static function onTitle(index:int,title:String,name:String):void
		{
			sectionList.push(new SectionVo(index,title,name));
		}
		
		public static function getSectionVo(index:int):SectionVo{
			return sectionList[index];
		}
		/**
		 * 检查车类型是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		public static function hasKindEName(eName:String):Boolean{
			for (var i:int = kindList.length - 1; i >= 0; i--) 
			{
				var kvo:KindVo = kindList[i] as KindVo;
				if(kvo.eName == eName)return true;
			}
			return false;
		}
		/**
		 * 存在的情况下就不必向服务器发送请求
		 * @return 
		 */		
		public static function get hasKind():Boolean{
			return kindList != null && kindList.length != 0;
		}
		
		public static var kindList:Vector.<KindVo> = new Vector.<KindVo>();
		//		kindList:Vector.<KindVo> = new Vector.<KindVo>();
		
		public static function addKindVo(obj:Object):void{
			var kvo:KindVo = new KindVo();
			for (var key:String in obj) 
			{
				kvo[key] = obj[key];
			}
			kindList.push(kvo);
		}
		
		//		public static function updateKindVo(obj:Object):void{
		//			var kvo:KindVo = getKindVo(obj.kindID);
		//			for (var key:String in obj) 
		//			{
		//				kvo[key] = obj[key];
		//			}
		////			kindList.refresh();//重新刷新视图
		//		}
		
		public static function getKindVo(id:int):KindVo{
			for (var i:int = kindList.length - 1; i >= 0; i--) 
			{
				var kvo:KindVo = kindList[i] as KindVo;
				if(kvo != null && kvo.kindID == id){
					return kvo;
				}
			}
			return null;
		}
		
		//		public static function deleteKindVo(id:int):void{
		//			for (var i:int = kindList.length - 1; i >= 0; i--) 
		//			{
		//				var kvo:KindVo = kindList.getItemAt(i) as KindVo;
		//				if(kvo != null && kvo.kindID == id){
		//					kindList.removeItemAt(i);
		//					break;
		//				}
		//			}
		//		}
		/**
		 * 检查车系别是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		//		public static function hasTypeEName(kindID:int,eName:String):Boolean{
		//			var typeList:ArrayCollection = getTypeList(kindID);
		//			for (var i:int = typeList.length - 1; i >= 0; i--) 
		//			{
		//				var tvo:TypeVo = typeList.getItemAt(i) as TypeVo;
		//				if(tvo.eName == eName)return true;
		//			}
		//			return false;
		//		}
		/**
		 * 获取相对kindID的车系数组
		 * @param kindID
		 * @return 
		 */		
		public static function getTypeList(kindID:int):Vector.<TypeVo>{
			if(typeDic[kindID] == null)typeDic[kindID] = new Vector.<TypeVo>();
			return typeDic[kindID];
		}
		
		//		[Bindable]
		/** 车系数组 */
		//		private static var typeList:ArrayCollection = new ArrayCollection();
		private static var typeDic:Dictionary = new Dictionary(true);
		
		public static function addTypeVo(obj:Object):void{
			var typeList:Vector.<TypeVo> = getTypeList(obj.kindID);
			var tvo:TypeVo = new TypeVo();
			for (var key:String in obj) 
			{
				tvo[key] = obj[key];
			}
			typeList.push(tvo);
		}
		
		//		public static function updateTypeVo(obj:Object):void{
		//			var typeList:ArrayCollection = getTypeList(obj.kindID);
		//			var tvo:TypeVo = getTypeVo(obj.typeID,obj.kindID);
		//			for (var key:String in obj) 
		//			{
		//				tvo[key] = obj[key];
		//			}
		//			typeList.refresh();//重新刷新视图
		//		}
		public static function getTypeVo(id:int,kindID:int):TypeVo{
			var typeList:Vector.<TypeVo> = getTypeList(kindID);
			for (var i:int = typeList.length - 1; i >= 0; i--) 
			{
				var tvo:TypeVo = typeList[i];
				if(tvo != null && tvo.typeID == id){
					return tvo;
				}
			}
			return null;
		}
		//		public static function deleteTypeVo(id:int,kindID:int):void{
		//			var typeList:ArrayCollection = getTypeList(kindID);
		//			for (var i:int = typeList.length - 1; i >= 0; i--) 
		//			{
		//				var tvo:TypeVo = typeList.getItemAt(i) as TypeVo;
		//				if(tvo != null && tvo.typeID == id){
		//					typeList.removeItemAt(i);
		//					break;
		//				}
		//			}
		//		}
		/**
		 * 检查车品牌是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		public static function hasCarEName(eName:String):Boolean{
			for (var i:int = carList.length - 1; i >= 0; i--) 
			{
				var cvo:CarVo = carList[i];
				if(cvo.eName == eName)return true;
			}
			return false;
		}
		public static var carList:Vector.<CarVo> = new Vector.<CarVo>();
		public static function addCarVo(obj:Object):void{
			var cvo:CarVo = new CarVo();
			for (var key:String in obj) 
			{
				cvo[key] = obj[key];
			}
			carList.push(cvo);
		}
		//		public static function updateCarVo(obj:Object):void{
		//			var cvo:CarVo = getCarVo(obj.carID);
		//			for (var key:String in obj) 
		//			{
		//				cvo[key] = obj[key];
		//			}
		//			carList.refresh();//重新刷新视图
		//		}
		public static function getCarVo(id:int):CarVo{
			for (var i:int = carList.length - 1; i >= 0; i--) 
			{
				var cvo:CarVo = carList[i];
				if(cvo != null && cvo.carID == id){
					return cvo;
				}
			}
			return null;
		}
		
		public static function getCarListByType(typeID:int):Vector.<CarVo>{
			var cList:Vector.<CarVo> = new Vector.<CarVo>();
			for each (var cvo:CarVo in carList) 
			{
				if(cvo.typeID == typeID){
					cList.push(cvo);
				}
			}
			return cList;
		}
		
		
		//		public static function deleteCarVo(id:int):void{
		//			for (var i:int = carList.length - 1; i >= 0; i--) 
		//			{
		//				var cvo:CarVo = carList.getItemAt(i) as CarVo;
		//				if(cvo != null && cvo.carID == id){
		//					carList.removeItemAt(i);
		//					break;
		//				}
		//			}
		//		}
		/**
		 * 检查车部件是否包含该英文名
		 * @param eName
		 * @return 
		 */		
		public static function hasPartEName(eName:String,
											carID:int,sectionID:int,itemID:String):Boolean{
			var partList:Vector.<PartVo> = getPartList(carID,sectionID,itemID);
			for (var i:int = partList.length - 1; i >= 0; i--) 
			{
				var pvo:PartVo = partList[i];
				if(pvo.eName == eName)return true;
			}
			return false;
		}
		/**
		 * 部件的三要素:车品牌 部件索引 部件条目索引 
		 */		
		private static var partDic:Dictionary = new Dictionary(true);
		/**
		 * 获取相对kindID的车系数组
		 * @param kindID
		 * @return 
		 */		
		public static function getPartList(carID:int,sectionID:int,itemID:String):Vector.<PartVo>{
			if(itemID == null)return null;//说明没有选中条目
			if(partDic[carID + '_' + sectionID + '_' + itemID] == null)
				partDic[carID + '_' + sectionID + '_' + itemID] = new Vector.<PartVo>();
			return partDic[carID + '_' + sectionID + '_' + itemID];
		}
		public static function addPartVo(obj:Object):PartVo{
			var pvo:PartVo = new PartVo();
			var partList:Vector.<PartVo> = getPartList(obj.carID,obj.sectionID,obj.itemID);
			for (var key:String in obj) 
			{
				if(key == 'textureList'){
					if(obj[key] != null && obj[key] != '')
						pvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
					//保存图片数组
				}else{
					pvo[key] = obj[key];
				}
			}
			partList.push(pvo);
			return pvo;
		}
		//		public static function updatePartVo(obj:Object):PartVo{
		//			var partList:ArrayCollection = getPartList(obj.carID,obj.sectionID,obj.itemID);
		//			var pvo:PartVo = getPartVo(obj.partID,obj.carID,obj.sectionID,obj.itemID);
		//			for (var key:String in obj) 
		//			{
		//				if(key == 'textureList'){
//		if(obj[key] != null && obj[key] != '')
//			pvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
//		//保存图片数组
//	}else{
//		pvo[key] = obj[key];
//	}
		//			}
		//			partList.refresh();
		//			return pvo;
		//		}
		public static function getPartVo(partID:int,carID:int,sectionID:int,itemID:String):PartVo{
			var partList:Vector.<PartVo> = getPartList(carID,sectionID,itemID);
			for (var i:int = partList.length - 1; i >= 0; i--) 
			{
				var pvo:PartVo = partList[i];
				if(pvo.partID == partID)return pvo;
			}
			return null;
		}
		//		public static function deletePartVo(partID:int,carID:int,sectionID:int,itemID:String):void{
		//			var partList:ArrayCollection = getPartList(carID,sectionID,itemID);
		//			for (var i:int = partList.length - 1; i >= 0; i--) 
		//			{
		//				var pvo:PartVo = partList.getItemAt(i) as PartVo;
		//				if(pvo != null && pvo.partID == partID){
		//					partList.removeItemAt(i);
		//					break;
		//				}
		//			}
		//		}
		public static function addMeshVo(obj:Object):MeshVo{
			var mvo:MeshVo = new MeshVo();
			for (var key:String in obj) 
			{
				if(key == 'textureList'){
					if(obj[key] != null && obj[key] != '')
						mvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
					//保存图片数组
				}else{
					mvo[key] = obj[key];
				}
			}
			var cvo:CarVo = getCarVo(obj.carID);
			cvo[obj.meshKey] = mvo;
			return mvo;
		}
//		public static function updateMeshVo(obj:Object):MeshVo{
//			var cvo:CarVo = getCarVo(obj.carID);
//			var mvo:MeshVo = cvo[obj.meshKey];
//			for (var key:String in obj) 
//			{
//				if(key == 'textureList'){
//					if(obj[key] != null && obj[key] != '')
//						mvo[key] = obj[key].split(NetConfig.ADDRESS_KEY);
//					//保存图片数组
//				}else{
//					mvo[key] = obj[key];
//				}
//			}
//			return mvo;
//		}
	}
}